﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKDimensionModel.LevelDimension
{
	public partial class LevelStyleForm : System.Windows.Forms.Form
	{
		public string TextFont
		{
			get
			{
				return this.cmbTextStyle.Text;
			}
			set
			{
				this.cmbTextStyle.Text = value.ToString();
			}
		}

		public double TextHeight
		{
			get
			{
				return LevelDimensionManager.StrToDouble(this.hyTbTextHeight.Text, 0.0);
			}
			set
			{
				this.cmbTextStyle.Text = value.ToString("f1");
			}
		}

		public double AspectRatio
		{
			get
			{
				return LevelDimensionManager.StrToDouble(this.hyTbAspectRatio.Text, 0.0);
			}
			set
			{
				this.hyTbAspectRatio.Text = value.ToString("f1");
			}
		}

		public int Precision
		{
			get
			{
				return this.cmbPrecision.SelectedIndex;
			}
			set
			{
				this.cmbTextStyle.SelectedIndex = value;
			}
		}

		public int TextAlignment
		{
			get
			{
				return this.cmbAlignment.SelectedIndex;
			}
			set
			{
				this.cmbAlignment.SelectedIndex = value;
			}
		}

		public LevelStyleForm(ExternalCommandData cmdData, LevelDimensionForm dlgMain)
		{
			this.InitializeComponent();
			this.m_cmdData = cmdData;
			this.m_dlgMain = dlgMain;
		}

		private void LevelStyleForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeControl();
			this.InitializeTextStyle();
			this.ReadData();
			this.SetControlVisible();
		}

		private void InitializeControl()
		{
			this.cmbPrecision.Items.Add("0.00");
			this.cmbPrecision.Items.Add("0.000");
			this.cmbPrecision.SelectedIndex = 0;
			this.cmbAlignment.Items.Add("左侧");
			this.cmbAlignment.Items.Add("中心");
			this.cmbAlignment.Items.Add("右侧");
			this.cmbAlignment.SelectedIndex = 0;
		}

        private void SetComboBoxSelectedIndex(System.Windows.Forms.ComboBox control, int index)
		{
			if (index < 0 || index >= control.Items.Count)
			{
				return;
			}
			control.SelectedIndex = index;
		}

		private void ReadData()
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "LevelStyleForm.dat";
			if (!File.Exists(path))
			{
				this.cmbTextStyle.Text = "黑体 4mm";
				this.hyTbTextHeight.Text = "3.5";
				this.hyTbAspectRatio.Text = "0.7";
				this.cmbPrecision.Text = "0.000";
				this.cmbAlignment.Text = "右侧";
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.cmbTextStyle.Text = streamReader.ReadLine();
			this.hyTbTextHeight.Text = LevelDimensionManager.StrToDouble(streamReader.ReadLine(), 0.0).ToString("f1");
			this.hyTbAspectRatio.Text = LevelDimensionManager.StrToDouble(streamReader.ReadLine(), 0.0).ToString("f1");
			int index = Convert.ToInt32(streamReader.ReadLine());
			this.SetComboBoxSelectedIndex(this.cmbPrecision, index);
			index = Convert.ToInt32(streamReader.ReadLine());
			this.SetComboBoxSelectedIndex(this.cmbAlignment, index);
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "LevelStyleForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cmbTextStyle.Text);
			streamWriter.WriteLine(this.hyTbTextHeight.Text);
			streamWriter.WriteLine(this.hyTbAspectRatio.Text);
			streamWriter.WriteLine(this.cmbPrecision.SelectedIndex);
			streamWriter.WriteLine(this.cmbAlignment.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void InitializeTextStyle()
		{
			this.cmbTextStyle.Items.Clear();
			InstalledFontCollection installedFontCollection = new InstalledFontCollection();
			FontFamily[] families = installedFontCollection.Families;
			if (families == null || families.Length < 1)
			{
				return;
			}
			foreach (FontFamily fontFamily in families)
			{
				this.cmbTextStyle.Items.Add(fontFamily.Name);
			}
			this.cmbTextStyle.SelectedIndex = 0;
		}

		private void SetControlVisible()
		{
			if (this.m_dlgMain.Automatic && this.m_dlgMain.Associate)
			{
				this.cmbPrecision.Enabled = false;
				this.cmbAlignment.Enabled = false;
				return;
			}
			this.cmbPrecision.Enabled = true;
			this.cmbAlignment.Enabled = true;
		}

		public DimensionStyleParameter GetDimensionStyleParam()
		{
			return new DimensionStyleParameter
			{
				m_strTextFont = this.TextFont,
				m_dTextHeight = this.TextHeight,
				m_dAspectRatio = this.AspectRatio,
				m_nPrecision = this.Precision,
				m_nTextAlign = this.TextAlignment
			};
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.SaveData();
		}

		private void hyTbAspectRatio_Validating(object sender, CancelEventArgs e)
		{
			if ("" == this.hyTbAspectRatio.Text)
			{
				return;
			}
			this.hyTbAspectRatio.Text = Convert.ToDouble(this.hyTbAspectRatio.Text).ToString("f1");
			double num = Convert.ToDouble(this.hyTbAspectRatio.Text);
			if (1E-06 >= num)
			{
				YJKMessageBox.Information("高宽比的输入范围不能小于0");
				e.Cancel = true;
			}
		}

		private ExternalCommandData m_cmdData;

		private LevelDimensionForm m_dlgMain;
	}
}
